package richbar.com.richbar.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.common.util.LogUtil;
import com.luck.picture.lib.entity.LocalMedia;
import com.okhttp.GlideUtil;

import java.util.List;

import richbar.com.richbar.R;
import richbar.com.richbar.activity.PreviewPicAc;
import richbar.com.richbar.help.Constants;
import richbar.com.richbar.view.dialog.TipDialog;

/**
 * Created by qyang on 2018-9-13.
 */

public class PhotoNewAdapter extends BaseAdapter{
    private List<String> urls;
    private List<LocalMedia> data;
    private Activity mActivity;
    private OnAddPicClickListener listener;
    private TipDialog tipDialog;
    private LayoutInflater inflater;

    public PhotoNewAdapter(Activity mActivity, List<String> urls, List<LocalMedia> data, OnAddPicClickListener listener) {
        this.data = data;
        this.urls = urls;
        this.mActivity = mActivity;
        this.listener = listener;
        inflater = LayoutInflater.from(mActivity);
        tipDialog = new TipDialog();
    }
    @Override
    public int getCount() {
        return data.size() == Constants.PHOTO_MAX ? data.size() : (data.size() + 1);
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_pic,parent,false);
        ImageView pic = view.findViewById(R.id.item_pic);
        ImageView delete = view.findViewById(R.id.delete);
        if (isShowAddItem(position)){
            pic.setImageResource(R.mipmap.photo_add);
            pic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onAddPicClick();
                }
            });
        }else {
            GlideUtil.loadImage(mActivity,urls.get(position),pic);
        }
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showIsDelete(position);
            }
        });
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PreviewPicAc.start(mActivity, urls,position);
            }
        });
        return view;
    }

    private void showIsDelete(final int position) {
        tipDialog.show(mActivity, "删除图片", new TipDialog.OnListener() {
            @Override
            public void onBesure() {
                data.remove(position);
                urls.remove(position);
                notifyDataSetChanged();
            }
        });
    }

    public List<LocalMedia> getData() {
        return data;
    }

    public void setData(List<LocalMedia> data) {
        this.data = data;
    }

    public List<String> getUrls() {
        return urls;
    }

    public void setUrls(List<String> urls) {
        this.urls = urls;
    }

    private boolean isShowAddItem(int position){
        int size = data.size() == 0 ? 0 : data.size();
        return position == size;
    }


    public interface OnAddPicClickListener{
        void onAddPicClick();
    }
}
